package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oracle.net.aso.b;
import pojo.Order;
import pojo.User;
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CartServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Map<String, Integer> num = (Map<String, Integer>) session.getAttribute("bnum");
		List<Order> orders = new ArrayList<Order>();
		Set<String> keys = num.keySet();
		for(String bid:keys){
			Order order = new Order();
			order.setId(UUID.randomUUID().toString());
			order.setUid(user.getId());
			order.setBid(bid);
			System.out.println("cart!!!!! key:"+bid);
			int count = num.get(bid);
			order.setNum(count);
			orders.add(order);
		}
		session.setAttribute("order", orders);
		
		request.getRequestDispatcher("../cart.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
